﻿/*
 * 由SharpDevelop创建。
 * 用户： Dandjinh Deng
 * 日期: 2018/8/1
 * 时间: 11:33
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TransferUI
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
		
		System.Text.Encoding coding = System.Text.Encoding.GetEncoding(936);
		
		void onDragDrop(object sender, DragEventArgs e)
		{
			var files = (string[])e.Data.GetData(DataFormats.FileDrop);
			int step = 100 / files.Length;
			progressBar1.Value = 0;
			foreach (var f in files) {
				try {
					var infile = new FileStream(f, FileMode.Open);
					var sr = new StreamReader(infile, coding);
					
					FileStream outfile = null;
					StreamWriter sw = null;
					while (true) {
						var s = sr.ReadLine();
						if (s == null)
							break;
						if (s.Contains("注册")) {
							if (sw == null) {
								var newname = f + ".txt";
								outfile = new FileStream(newname, FileMode.OpenOrCreate);
								sw = new StreamWriter(outfile);
							}
							sw.WriteLine(s);
						}
					}
					
					if (sw != null) {
						sw.Close();
						outfile.Close();
					}
					sr.Close();
					infile.Close();
				} catch (Exception ex) {
					MessageBox.Show(ex.Message, "错误");
				}
				
				progressBar1.Value += step;
			}
			progressBar1.Value = 100;
		}
		
		void onDragEnter(object sender, DragEventArgs e)
		{
			e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Link : DragDropEffects.None;
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			SetWindowPos(Handle, -1, 0, 0, 0, 0, 1 | 2);
		}
	}
}
